package com.jumlabs.services.auth;


import java.util.List;
import com.jumlabs.entities.auth.User;
import com.jumlabs.repositories.auth.UserRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.PageRequest;

public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserRepository userRepository;	
	@Transactional(readOnly=true)
	public Page<User> getUsersPaginated(int page,int rows){
		PageRequest pageRequest=new PageRequest(page, rows);
				return userRepository.findAll(pageRequest);
	}
	
	@Transactional(readOnly=true)
	public List<User> getUsers() {	
		return userRepository.findAll();		
	}
	
	@Transactional(readOnly=true)
	public User findUserById(int userID) {
		return userRepository.findOne(userID);
	}
	
	
	@Transactional
	public void save(User user){
	  userRepository.save(user);
	}
	
	@Transactional
	public void update(User user) {
		userRepository.save(user);
		
	}
	
	@Transactional
	public void delete(int userID) {
	  userRepository.delete(userID);
	}
	
	@Transactional
	public void delete(User user) {
		userRepository.delete(user);
		
	}

	public boolean exists(String userName) {
	
		List<User> result=userRepository.findByUserName(userName);
		if(result!=null&&result.size()>0)
			return true;
		return false;
	}

	public User findUserByUserName(String userName) {
		List<User> result=userRepository.findByUserName(userName);
		if(result!=null&&result.size()>0)
			return result.get(0);
		return null;
	}

}
